<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>列出指定路径中的文件和目录</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.rewinddir.html">« rewinddir</a></li>
      <li style="float: right;"><a href="book.fileinfo.html">Fileinfo »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.dir.html">目录函数函数</a></li>
    <li>列出指定路径中的文件和目录</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.scandir" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">scandir</h1>
  <p class="verinfo">(PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">scandir</span> &mdash; <span class="dc-title">列出指定路径中的文件和目录</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.scandir-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>scandir</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$directory</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$sorting_order</code><span class="initializer"> = <strong><code>SCANDIR_SORT_ASCENDING</code></strong></span></span>, <span class="methodparam"><span class="type">?</span><span class="type"><span class="type">resource</span><span class="type"></span></span> <code class="parameter">$context</code><span class="initializer"> = <strong><code>null</code></strong></span></span>): <span class="type"><span class="type">array</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   返回 <span class="type">array</span>，包含有
   <code class="parameter">directory</code> 中的文件和目录。
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.scandir-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">directory</code></dt>

     <dd>

      <p class="para">
       要被浏览的目录
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">sorting_order</code></dt>

     <dd>

      <p class="para">
       默认的排序顺序是按字母升序排列。如果使用了可选参数
       <code class="parameter">sorting_order</code>（设为
       1），则排序顺序是按字母降序排列。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">context</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">context</code> 参数的说明见手册中的
       <a href="ref.stream.html" class="link">Streams API</a> 一章。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.scandir-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功则返回包含有文件名的 <span class="type">array</span>，如果失败则返回
   <strong><code>false</code></strong>。如果 <code class="parameter">directory</code>
   不是个目录，则返回布尔值 <strong><code>false</code></strong> 并生成一条
   <strong><code>E_WARNING</code></strong> 级的错误。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.scandir-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       现在 <code class="parameter">context</code> 允许为 null。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.scandir-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2132">
    <p><strong>示例 #1 简单的 <span class="function"><strong>scandir()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dir&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'/tmp'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$files1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">scandir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dir</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$files2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">scandir</span><span style="color: #007700">(</span><span style="color: #0000BB">$dir</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SCANDIR_SORT_DESCENDING</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$files1</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$files2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; .
    [1] =&gt; ..
    [2] =&gt; bar.php
    [3] =&gt; foo.txt
    [4] =&gt; somedir
)
Array
(
    [0] =&gt; somedir
    [1] =&gt; foo.txt
    [2] =&gt; bar.php
    [3] =&gt; ..
    [4] =&gt; .
)
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.scandir-notes">
  <h3 class="title">注释</h3>
  <div class="tip"><strong class="tip">小技巧</strong><p class="simpara">如已启用<a href="filesystem.configuration.html#ini.allow-url-fopen" class="link">fopen 包装器</a>，在此函数中， URL 可作为文件名。关于如何指定文件名详见 <span class="function"><a href="function.fopen.html" class="function">fopen()</a></span>。各种
wapper 的不同功能请参见 <a href="wrappers.html" class="xref">支持的协议和封装协议</a>，注意其用法及其可提供的预定义变量。</p></div>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.scandir-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.opendir.html" class="function" rel="rdfs-seeAlso">opendir()</a> - 打开目录句柄</span></li>
    <li class="member"><span class="function"><a href="function.readdir.html" class="function" rel="rdfs-seeAlso">readdir()</a> - 从目录句柄中读取条目</span></li>
    <li class="member"><span class="function"><a href="function.glob.html" class="function" rel="rdfs-seeAlso">glob()</a> - 寻找与模式匹配的文件路径</span></li>
    <li class="member"><span class="function"><a href="function.is-dir.html" class="function" rel="rdfs-seeAlso">is_dir()</a> - 判断给定文件名是否是一个目录</span></li>
    <li class="member"><span class="function"><a href="function.sort.html" class="function" rel="rdfs-seeAlso">sort()</a> - 对数组升序排序</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>